home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalToggleButtonUI.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  149 lines

  1. /*
  2.  * @(#)MetalToggleButtonUI.java    1.8 98/04/13
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.plaf.basic.BasicToggleButtonUI;
  26.  
  27. import com.sun.java.swing.*;
  28. import com.sun.java.swing.border.*;
  29. import com.sun.java.swing.plaf.*;
  30. import com.sun.java.swing.*;
  31. import com.sun.java.swing.plaf.basic.BasicGraphicsUtils;
  32.  
  33. import java.io.Serializable;
  34.  
  35. /**
  36.  * MetalToggleButton implementation
  37.  * <p>
  38.  * Warning: serialized objects of this class will not be compatible with
  39.  * future swing releases.  The current serialization support is appropriate
  40.  * for short term storage or RMI between Swing1.0 applications.  It will
  41.  * not be possible to load serialized Swing1.0 objects with future releases
  42.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  43.  * baseline for the serialized form of Swing objects.
  44.  *
  45.  * @version 1.8 04/13/98
  46.  * @author Tom Santos
  47.  */
  48. public class MetalToggleButtonUI extends BasicToggleButtonUI {
  49.     private static Color selectedColor;
  50.     private static Color disabledTextColor;
  51.     private static Color focusColor;
  52.  
  53.     protected final static Insets defaultMargin = new Insets(2,14,2,14);
  54.  
  55.     private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
  56.  
  57.     public static ComponentUI createUI(JComponent b) {
  58.         return metalToggleButtonUI;
  59.     }
  60.  
  61.     public void installUI(JComponent c) {
  62.         super.installUI(c);
  63.     selectedColor = UIManager.getColor("ToggleButton.selected");
  64.     disabledTextColor = UIManager.getColor("ToggleButton.disabledText");
  65.     focusColor = UIManager.getColor("ToggleButton.focus");
  66.     }
  67.  
  68.     protected void paintButtonPressed(Graphics g, AbstractButton b) {
  69.         Dimension size = b.getSize();
  70.     g.setColor( selectedColor );
  71.     g.fillRect( 0, 0, size.width, size.height );
  72.     }
  73.  
  74.     public Insets getDefaultMargin( AbstractButton b ) {
  75.         return defaultMargin;
  76.     }
  77.  
  78.     protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  79.     AbstractButton b = (AbstractButton) c;                 
  80.     ButtonModel model = b.getModel();
  81.     FontMetrics fm = g.getFontMetrics();
  82.  
  83.     /* Draw the Text */
  84.     if(model.isEnabled()) {
  85.         /*** paint the text normally */
  86.         g.setColor(b.getForeground());
  87.         BasicGraphicsUtils.drawString(g,text, model.getMnemonic(),
  88.                       textRect.x,
  89.                       textRect.y + fm.getAscent());
  90.     }
  91.     else {
  92.         /*** paint the text disabled ***/
  93.         if (model.isSelected())
  94.         g.setColor(UIManager.getColor("Button.background"));
  95.         else
  96.             g.setColor(UIManager.getColor("Button.disabledText"));
  97.         BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  98.                       textRect.x, textRect.y + fm.getAscent());
  99.  
  100.     }
  101.     }
  102.  
  103.     static class MetalToggleButtonBorder extends MetalButtonBorder {
  104.         public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  105.         JToggleButton button = (JToggleButton)c;
  106.         ButtonModel model = button.getModel();
  107.  
  108.         if (! c.isEnabled() ) {
  109.             MetalUtils.drawDisabledBorder(g, x, y, w, h);
  110.         } else {
  111.             if ( model.isPressed() && model.isArmed() ) {
  112.             MetalUtils.drawPressed3DBorder( g, x, y, w, h );
  113.         } else if ( model.isSelected() ) {
  114.             MetalUtils.drawDark3DBorder( g, x, y, w, h );
  115.         } else {
  116.             MetalUtils.drawFlush3DBorder( g, x, y, w, h );
  117.         }
  118.         }
  119.     }
  120.     }
  121.  
  122.     protected void paintFocus(Graphics g, AbstractButton b,
  123.                   Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  124.  
  125.         Rectangle focusRect = new Rectangle();
  126.     String text = b.getText();
  127.     boolean isIcon = b.getIcon() != null;
  128.  
  129.         // If there is text
  130.         if ( text != null & !text.equals( "" ) ) {
  131.           if ( !isIcon ) {
  132.             focusRect.setBounds( textRect );
  133.         }
  134.         else {
  135.             focusRect.setBounds( iconRect.union( textRect ) );
  136.         }
  137.         }
  138.         // If there is an icon and no text
  139.         else if ( isIcon ) {
  140.           focusRect.setBounds( iconRect );
  141.         }
  142.  
  143.         g.setColor(focusColor);
  144.     g.drawRect((focusRect.x-1), (focusRect.y-1),
  145.           focusRect.width+1, focusRect.height+1);
  146.     
  147.     }
  148. }
  149.